package nxu.it.controller;
import nxu.it.model.ApiResult;
import nxu.it.model.Todo;
import nxu.it.service.GreetingService;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.boot.web.MimeType;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.Render;
import org.objectweb.asm.Handle;


import java.util.ArrayList;
import java.util.*;


@Controller
public class TodoController {

    static List<Todo> todoList = new ArrayList<>();

    @Mapping(value = "todo/list", method = MethodType.GET, produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("todo/list.ftl");
        mv.put("todoList", todoList);
        return mv;
    }

    @Mapping(value = "todo/save", method = MethodType.POST, produces = MimeType.TEXT_HTML_VALUE)
    public void save(Context ctx, String content) {
        Todo todo = new Todo(content);
        todoList.add(todo);

        // 保存成功后重定向到 /todo/list
        ctx.redirect("/todo/list");
    }

    @Mapping(value = "/todo/check", method = MethodType.GET, produces = MimeType.TEXT_HTML_VALUE)
    public String check(Context ctx) {
        // 遍历 todoList 查找指定 ID 的 Todo 对象
        String id = ctx.param("id");
        System.out.println(id);
        boolean found = false;
        for (Todo todo : todoList) {
            if (todo.getId().toString().equals(id)) {
                // 切换完成状态
                todo.setComplete(!todo.getComplete());
                // 重定向到 /todo/list
                ctx.redirect("/todo/list");
                found = true;
                return "id";

            }
        }

        // 如果指定的待办事项不存在，则显示错误信息
        if (!found) {
            return "wrong";

        }

        return " ";
    }

    @Mapping(value = "/todo/edit", method = MethodType.GET, produces = MimeType.TEXT_HTML_VALUE)
    public ModelAndView edit(String id, Context ctx) {
        Optional<Todo> todo = todoList.stream().filter(t -> t.getId().toString().equals(id)).findAny();
        if (todo.isEmpty()) {
            return new ModelAndView();
        }

        ModelAndView mv = new ModelAndView("todo/edit.ftl", Map.of("todo", todo.get()));
        return mv;
    }

    @Mapping(value = "/todo/update", method = MethodType.POST, produces = MimeType.TEXT_HTML_VALUE)
    public String updateTodo(Todo todo, Context ctx) {
        String complete=ctx.param("complete");
        if(complete==null)
        {
            ctx.redirect("/todo/list");
        }
        else
            for (Todo todos : todoList) {
            if (todo.getId().toString().equals(todos.getId().toString())) {
                // 切换完成状态
                todos.setComplete(!todos.getComplete());
                // 重定向到 /todo/list
                ctx.redirect("/todo/list");

                return complete;

            }

        }
        return "";
    }

    @Mapping(value = "/todo/delete", method = MethodType.GET, produces = MimeType.TEXT_HTML_VALUE)
    public String deleteTodo(String id, Context ctx) {

        for (Todo todo : todoList) {
            if (todo.getId().toString().equals(id)) {
                // 切换完成状态
                todoList.remove(todo);
                // 重定向到 /todo/list
                ctx.redirect("/todo/list");

                return "id";

            }
        }
        return "";
    }
}
